/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;

/**
 * @author Susanta Tewari
 * @version 1.0 Dec 6, 2010
 */
public class FormatUtil {

    private static DecimalFormat df1 = new DecimalFormat("0.####E0");
    private static DecimalFormat df2 = new DecimalFormat("0.###");

    private static DecimalFormat integerGroupingFormat = new DecimalFormat();

    {
        integerGroupingFormat.setGroupingSize(3);
    }

    /**
     * Formats using integer grouping decimal format of size 3. if length is greater than 10, it
     * uses decimal format of the form: 0.####E0.
     *
     * @param value value to be formatted
     * @return formatted value
     */
    public static String format(BigInteger value) {

        if (value.toString().length() > 10) {
            return df1.format(value);
        } else {
            return integerGroupingFormat.format(value);
        }
    }

    /**
     * Formats using integer grouping decimal format of size 3.
     *
     * @param value value to be formatted
     * @return formatted value
     */
    public static String format(Integer value) {
        return integerGroupingFormat.format(value);
    }

    /**
     * Formats using integer grouping decimal format of size 3.
     *
     * @param value value to be formatted
     * @return formatted value
     */
    public static String format(Long value) {
        return integerGroupingFormat.format(value);
    }

    /**
     * @param value
     *
     * @return
     */
    public static String format(BigDecimal value) {
        if(value.toString().length() <= 4) return df2.format(value);
        return df1.format(value);
    }

    /**
     * Method description
     *
     *
     * @param value
     *
     * @return
     */
    public static String format(Double value) {
        if(value.toString().length() <= 4) return df2.format(value);
        return df1.format(value);
    }
}
